package com.wutong.teacher_server.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wutong.common_server.result.DataTablePageDto;
import com.wutong.common_server.result.Response;
import com.wutong.common_server.utils.ResponseUtil;
import com.wutong.teacher_server.entity.EduTeacher;
import com.wutong.teacher_server.entity.EduTeacherTest;
import com.wutong.teacher_server.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wutong
 * @since 2020-10-22
 */
@RestController
@RequestMapping("/eduTeacher")
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;


    @PostMapping("/getTeachers")
    @ApiOperation("分页查询所有的老师")
    public Response getTeachers(@RequestBody EduTeacher eduTeacher){

        Integer pageIndex = eduTeacher.getPageIndex();
        Integer pageSize = eduTeacher.getPageSize();
        if(pageIndex == null){
            pageIndex = 1;//默认查第一页
        }
        if(pageSize == null){
            pageSize = 10;//默认查10条数据
        }
        Page<EduTeacher> page = new Page<>(pageIndex, pageSize);
        Page<EduTeacher> teachers = eduTeacherService.getTeachers(eduTeacher, page);

        return ResponseUtil.success(new DataTablePageDto<EduTeacher>(teachers));

    }

    @GetMapping("/getTeacherById")
    public Response<EduTeacher> getTeacherById(@RequestParam(value = "id",defaultValue = "1") String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResponseUtil.success(teacher);
    }


    @GetMapping("/deleteTeacher")
    @ApiOperation("根据id删除老师")
    public boolean deleteTeacher(@RequestParam(value = "id",defaultValue = "1") String id){

        boolean flag = eduTeacherService.removeById(id);
        return flag;
    }

    @PostMapping("/addTeacher")
    @ApiOperation("新增老师")
    public Response addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.addTeacher(eduTeacher);
        if(b){
            return ResponseUtil.success(b);
        }
        return ResponseUtil.failure(b);
    }

    @PostMapping("/updateTeacher")
    @ApiOperation("修改老师")
    public Response updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateTeacher(eduTeacher);
        if(b){
            return ResponseUtil.success(b);
        }
        return ResponseUtil.failure(b);
    }

    @GetMapping("/testDb")
    public void testDb(){


        List<EduTeacherTest> list = eduTeacherService.getList();
        ResponseUtil.success(list);
    }
}

